<?php
// $Id: store.php 180 2012-02-26 06:32:20Z tomqin@gmail.com $

defined('IN_ADMIN') or exit('Access Denied!');

class Store_Controller extends AdminController{

	function init(){
		$this->module = Base::loadModule('admin/store', true);
		$this->zone   = Base::loadModule('zone', true);
		$this->zone->initCity();
	}

	function onIndex(){

		$output = $this->module->listStore($_GET);
		list($total, $data, $pagestring) = $output;

		$this->setVar('name', array_get_string($_GET, 'name'));
		$this->setVar('zone_id', array_get_int($_GET, 'zone_id'));
		$this->setVar('subzone_id', array_get_int($_GET, 'subzone_id'));

		$this->setVar('zone', $this->zone);
		$this->setVar('total', $total);
		$this->setVar('data', $data);
		$this->setVar('pagestring', $pagestring);
		$this->setVar('action', $this->action);
		$this->render('admin/store_list');
	}

	function onCreate(){
		$this->setVar('action', $this->action);
		$this->render('admin/store_edit');
	}

	function onModify(){
		$res_id = intval($_GET['res_id']);
		$data = $this->module->getStore($res_id);
		$data['service_time'] = explode('-', $data['service_time']);
		$data['delivery_time'] = preg_split('/[\s-]+/', $data['delivery_time']);

		$deliver = $this->module->getDeliver($res_id);

		$this->setVar('data', $data);
		$this->setVar('deliver', $deliver);

		$this->setVar('zone_id', $data['zone_id']);
		$this->setVar('subzone_id', $data['subzone_id']);
		$this->setVar('action', $this->action);
		$this->render('admin/store_edit');
	}

	function onProcess(){
		$operate = array(
			'返回列表页' => Base::AdminUrl($this->controller, 'index'),
		);
		switch(array_get_string($_POST, 'action')){
			case 'create':
				$this->checkData($_POST);
				$this->module->addStore($_POST);
				$message = '餐厅信息添加成功！';
				$operate = array(
					'继续添加信息' => Base::adminUrl($this->controller, 'create'),
					'返回列表页'   => Base::adminUrl($this->controller, 'index'),
				);
				break;
			case 'modify':
				$this->checkData($_POST);
				$this->module->editStore(array_get_int($_POST, 'res_id'), $_POST);
				$message = '餐厅信息更新成功！';
				break;
			case 'delete':
				$this->module->delStore(array_get_int($_POST, 'res_id'));
				$message = '餐厅信息删除成功！';
				break;
		}

		$this->renderDialog($message, $operate);
	}

	function onBatch(){
		$sorts = array();
		if(isset($_POST['select']) && is_array($_POST['select'])){
			foreach($_POST['select'] as $res_id){
				$sorts[$res_id] = intval($_POST['listorder'][$res_id]);
			}
		}

		switch(array_get_string($_POST, 'action')){
			case 'sort':
				$this->module->sortStoreArray($sorts);
				$message = '餐厅信息批量排序设置成功！';
				break;
			case 'enable':
				$this->module->statStoreArray(array_keys($sorts), 1);
				$message = '餐厅信息批量审核操作成功！';
				break;
			case 'disable':
				$this->module->statStoreArray(array_keys($sorts), 0);
				$message = '餐厅信息取消审核操作成功！';
				break;
			case 'delete':
				$this->module->delStoreArray(array_keys($sorts));
				$message = '餐厅信息删除成功！';
				break;
		}

		$this->renderDialog($message, array('返回列表页' => Base::AdminUrl($this->controller, 'index')));
	}

	private function checkData(){

	}
}
?>